Ext.define('MyApp.controller.Registro', {
    extend: 'Ext.app.Controller',
    views: [
        'registro.RegistroForm',
    ],
    refs: [{
            ref: 'registroForm',
            selector: '#registromain'
        }],
    init: function() {
        var me = this;
        me.control({
            '#registromain button[action=save]': {
                click: me.save
            },
            '#registromain button[action=new]': {
                click: me.clearForm
            },
            '#registromain button[action=buscar]': {
                click: me.buscarPersona
            },
            '#registromain textfield[name=cedula]': {
                specialkey: me.buscar
            },
            '#registromain combobox[name=codTlf1]': {
                select: me.selectCodigoCelular
            },
            '#registromain combobox[name=codTlf2]': {
                select: me.selectCodigoLocal
            },
            '#registromain combobox[name=estado]': {
                select: me.selectEstado
            },
            '#registromain combobox[name=municipio]': {
                select: me.selectMunicipio
            },
            '#registromain radiogroup[name=registros]': {
                change: me.changeRegistro
            }
        });
    },
    buscarPersona: function() {
        var nacionalidad = this.getRegistroForm().down('combo[name=nacionalidad]').value;
        cedula = this.getRegistroForm().down('textfield[name=cedula]').value;
        store = Ext.create('MyApp.store.Persona');
        form = this.getRegistroForm();
        store.proxy.extraParams.nacionalidad = nacionalidad;
        store.proxy.extraParams.cedula = cedula;
        this.getRegistroForm().down('button[action=buscar]').setDisabled(true);
        var myMask = new Ext.LoadMask(form, {msg: "Please wait..."});
        myMask.show();
        store.load(function(records, operation, success) {
            var campoNombre = Ext.ComponentQuery.query('#registromain textfield[name=nombre]')[0];
            if (store.count() > 0) {
                store.each(function(record) {//Step 3
                    Ext.ComponentQuery.query('#registromain fieldcontainer[name=responsable]')[0].setDisabled(0);
                    Ext.ComponentQuery.query('#registromain fieldset[name=geoiglesia]')[0].setDisabled(0);
                    Ext.ComponentQuery.query('#registromain fieldset[name=infoiglesia]')[0].setDisabled(0);
                    if (record.data.nombre != "") {
                        campoNombre.setReadOnly(1);
                        Ext.ComponentQuery.query('#registromain combobox[name=codTlf1]')[0].focus(false, 200);
                    } else {
                        campoNombre.setReadOnly(0);
                        campoNombre.focus(1);
                    }
                    ;
                    form.getForm().loadRecord(record);
                    if (record.data.celular != '') {
                        Ext.ComponentQuery.query('#registromain textfield[name=celular]')[0].setDisabled(0);
                    } else {
                        Ext.ComponentQuery.query('#registromain textfield[name=celular]')[0].setDisabled(1);
                    }
                    if (record.data.local != '') {
                        Ext.ComponentQuery.query('#registromain textfield[name=local]')[0].setDisabled(0);
                    } else {
                        Ext.ComponentQuery.query('#registromain textfield[name=local]')[0].setDisabled(1);
                    }
                    if (record.data.nrorif == 0) {
                        Ext.ComponentQuery.query('#registromain combobox[name=rif]')[0].setValue('V');
                        Ext.ComponentQuery.query('#registromain textfield[name=nrorif]')[0].reset();
                    }
                    if (record.data.nrorif2 == 0) {
                        Ext.ComponentQuery.query('#registromain combobox[name=rif2]')[0].setValue('V');
                        Ext.ComponentQuery.query('#registromain textfield[name=nrorif2]')[0].reset();
                    }
                    var parroquia = Ext.ComponentQuery.query('#registromain combobox[name=parroquia]')[0].setDisabled(0),
                            storeMunicipio = Ext.ComponentQuery.query('#registromain combobox[name=municipio]')[0].getStore(),
                            storeParroquia = Ext.ComponentQuery.query('#registromain combobox[name=parroquia]')[0].getStore();
                    storeMunicipio.proxy.extraParams.estado = record.data.estado;
                    storeMunicipio.load();
                    storeParroquia.proxy.extraParams.municipio = record.data.municipio;
                    storeParroquia.load();
                });
            } else {
                campoNombre.setReadOnly(0);
                campoNombre.focus(1);
                Ext.ComponentQuery.query('#registromain fieldcontainer[name=responsable]')[0].setDisabled(0);
                Ext.ComponentQuery.query('#registromain fieldset[name=geoiglesia]')[0].setDisabled(0);
                Ext.ComponentQuery.query('#registromain fieldset[name=infoiglesia]')[0].setDisabled(0);
                Ext.ComponentQuery.query('#registromain textfield[name=nombre]')[0].reset();
                Ext.ComponentQuery.query('#registromain combobox[name=codTlf1]')[0].reset();
                Ext.ComponentQuery.query('#registromain textfield[name=celular]')[0].reset();
                Ext.ComponentQuery.query('#registromain textfield[name=celular]')[0].setDisabled(1);
                Ext.ComponentQuery.query('#registromain combobox[name=codTlf2]')[0].reset();
                Ext.ComponentQuery.query('#registromain textfield[name=local]')[0].reset();
                Ext.ComponentQuery.query('#registromain textfield[name=local]')[0].setDisabled(1);
                Ext.ComponentQuery.query('#registromain textfield[name=email]')[0].reset();
                //Ext.ComponentQuery.query('#registromain combobox[name=estado]')[0].clearValue();
                Ext.ComponentQuery.query('#registromain combobox[name=municipio]')[0].reset();
                Ext.ComponentQuery.query('#registromain combobox[name=parroquia]')[0].reset();
                Ext.ComponentQuery.query('#registromain combobox[name=parroquia]')[0].setDisabled(1);
                Ext.ComponentQuery.query('#registromain textarea[name=direccion]')[0].reset();
            }
            myMask.hide();
        });
    },
    save: function() {
        var form = this.getRegistroForm();
        var me = this;
        form.getForm().submit({
            waitMsg: 'Espere por favor',
            waitTitle: 'Enviando datos',
            method: 'POST',
            url: BASE_PATH + 'index.php/persona/insertPersona',
            params: form.getForm().getValues(),
            success: function(form, action) {

                //Ext.MessageBox.show({ title: 'Informaci&oacute;n', msg: action.result.msg, buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.INFO});
                Ext.Msg.alert('Informaci&oacute;n', action.result.msg, function(btn) { //Step 2
                    if (btn === 'ok' || btn === 'cancel') {
                        //me.salir();
                        me.clearForm();
                    }
                });
            },
            failure: function(form, action) {
                switch (action.failureType) {
                    case Ext.form.Action.CLIENT_INVALID:
                        Ext.MessageBox.show({title: 'Error', msg: 'Algunos campos no fueron introducidos correctamente', buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.ERROR});
                        break;
                    case Ext.form.Action.CONNECT_FAILURE:
                        Ext.MessageBox.show({title: 'Error', msg: 'Error en comunicaci&oacute;n Ajax', buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.ERROR});
                        break;
                    case Ext.form.Action.SERVER_INVALID:
                        Ext.MessageBox.show({title: 'Error', msg: action.result.msg, buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.ERROR});
                        break;
                    default:
                        Ext.MessageBox.show({title: 'Alerta', msg: action.result.msg, buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.WARNING});
                }
            }
        });
    },
    clearForm: function() {
        var form = this.getRegistroForm();
        form.getForm().reset();
        Ext.ComponentQuery.query('#registromain textfield[name=cedula]')[0].focus(false, 200);
        Ext.ComponentQuery.query('#registromain textfield[name=nombre]')[0].setReadOnly(1);
        Ext.ComponentQuery.query('#registromain textfield[name=celular]')[0].setDisabled(1);
        Ext.ComponentQuery.query('#registromain textfield[name=local]')[0].setDisabled(1);
        Ext.ComponentQuery.query('#registromain fieldcontainer[name=responsable]')[0].setDisabled(1);
        Ext.ComponentQuery.query('#registromain fieldset[name=geoiglesia]')[0].setDisabled(1);
        Ext.ComponentQuery.query('#registromain fieldset[name=infoiglesia]')[0].setDisabled(1);
    },
    buscar: function(field, e) {
        if (e.getKey() == Ext.EventObject.ENTER || e.getKey() == Ext.EventObject.TAB) {
            if (field.getValue() != '' && field.isValid()) {
                this.buscarPersona();
            } else {
                Ext.MessageBox.show({title: 'Informaci&oacute;n', msg: 'Verifique el campo cédula.', buttons: Ext.MessageBox.OK, icon: Ext.MessageBox.INFO});
            }
        }
    },
    selectCodigoCelular: function() {
        Ext.ComponentQuery.query('#registromain textfield[name=celular]')[0].setDisabled(0);
        Ext.ComponentQuery.query('#registromain textfield[name=celular]')[0].focus(false, 200);
    },
    selectCodigoLocal: function() {
        var codigo = Ext.ComponentQuery.query('#registromain combobox[name=codTlf2]')[0].getValue();
        if (codigo == 'Ninguno') {
            Ext.ComponentQuery.query('#registromain combobox[name=codTlf2]')[0].reset();
            Ext.ComponentQuery.query('#registromain textfield[name=local]')[0].reset();
            Ext.ComponentQuery.query('#registromain textfield[name=local]')[0].setDisabled(1);
        } else {
            Ext.ComponentQuery.query('#registromain textfield[name=local]')[0].setDisabled(0);
            Ext.ComponentQuery.query('#registromain textfield[name=local]')[0].focus(false, 200);
        }
    },
    selectEstado: function() {
        var estado = Ext.ComponentQuery.query('#registromain combobox[name=estado]')[0].getValue();
        var municipio = Ext.ComponentQuery.query("#registromain combobox[name=municipio]")[0];
        var parroquia = Ext.ComponentQuery.query("#registromain combobox[name=parroquia]")[0];
        municipio.clearValue();
        parroquia.clearValue();
        parroquia.setDisabled(1);
        var store = municipio.getStore();
        store.proxy.extraParams.estado = estado;
        store.load();
    },
    selectMunicipio: function() {
        var municipio = Ext.ComponentQuery.query('#registromain combobox[name=municipio]')[0].getValue();
        var parroquia = Ext.ComponentQuery.query("#registromain combobox[name=parroquia]")[0];
        parroquia.clearValue();
        parroquia.setDisabled(0);
        var store = parroquia.getStore();
        store.proxy.extraParams.municipio = municipio;
        store.load();
    },
    changeRegistro: function(grupo, cmp) {
        var radio = cmp;
        if (radio.registro == 1) {
            Ext.ComponentQuery.query('#registromain combobox[name=rif2]')[0].setDisabled(0);
            Ext.ComponentQuery.query('#registromain textfield[name=nrorif2]')[0].setDisabled(0);
            Ext.ComponentQuery.query('#registromain combobox[name=rif2]')[0].reset();
            Ext.ComponentQuery.query('#registromain textfield[name=nrorif2]')[0].reset();
        } else if (radio.registro == 0) {
            Ext.ComponentQuery.query('#registromain combobox[name=rif2]')[0].setDisabled(1);
            Ext.ComponentQuery.query('#registromain textfield[name=nrorif2]')[0].setDisabled(1);
            Ext.ComponentQuery.query('#registromain combobox[name=rif2]')[0].reset();
            Ext.ComponentQuery.query('#registromain textfield[name=nrorif2]')[0].reset();
        } else if (radio.registro == 2) {
            Ext.ComponentQuery.query('#registromain combobox[name=rif2]')[0].setDisabled(1);
            Ext.ComponentQuery.query('#registromain textfield[name=nrorif2]')[0].setDisabled(1);
            Ext.ComponentQuery.query('#registromain combobox[name=rif2]')[0].reset();
            Ext.ComponentQuery.query('#registromain textfield[name=nrorif2]')[0].reset();
        }
    }
});


